﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Medical.Events;
using Sector7.Medical.Consts;

namespace Sector7.Medical.Controls
{
    /// <summary>
    /// NavigationStripItem.xaml 的交互逻辑
    /// </summary>
    public partial class NavigationStripItem : UserControl
    {
        #region Accessor
        public event ExpandedChangeEventHandler ExpandedChange = null;

        public event ClickNavigationItemEventHandler ClickNavigationItem = null;
        #endregion

        #region Public functions
        public void InitializeControl(NavigationConfiguration navigationConfig)
        {
            navigationHeader.InitializeControl(navigationConfig);
            if (navigationConfig.Items != null)
            {
                m_delta = navigationConfig.Items.Count * C_ControlSize.NAVIGATION_ITEM_HEIGHT;

                if (m_delta > 0)
                {
                    stpMain.Height = m_delta;
                    foreach (var item in navigationConfig.Items)
                    {
                        var navigationItem = new NavigationItem();
                        navigationItem.InitializeControl(item);
                        navigationItem.Height = C_ControlSize.NAVIGATION_ITEM_HEIGHT;
                        navigationItem.ExpandedChange += new ExpandedChangeEventHandler(Item_ExpandedChange);
                        navigationItem.ClickNavigationItem += new ClickNavigationItemEventHandler(Item_ClickNavigationItem);

                        stpMain.Children.Add(navigationItem);
                    }
                }
            }
        }
        #endregion

        #region Private functions
        private void Item_ExpandedChange(object sender, ExpandedChangeEventArgs e)
        {
            if (e.IsExpand)
            {
                m_delta += e.Delta;
                stpMain.Height += e.Delta;
                this.Height += e.Delta;
            }
            else
            {
                m_delta -= e.Delta;
                stpMain.Height -= e.Delta;
                this.Height -= e.Delta;
            }

            OnExpandedChange(sender, e);
        }

        void Item_ClickNavigationItem(object sender, ClickNavigationItemEventArgs e)
        {
            OnClickNavigationItem(sender, e);
        }

        private void OnExpandedChange(object sender, ExpandedChangeEventArgs e)
        {
            if (ExpandedChange != null)
            {
                ExpandedChange(sender, e);
            }
        }

        private void OnClickNavigationItem(object sender, ClickNavigationItemEventArgs e)
        {
            if (ClickNavigationItem != null)
            {
                ClickNavigationItem(sender, e);
            }
        }

        private void Header_ExpandedChange(object sender, ExpandedChangeEventArgs e)
        {
            if (e.IsExpand)
            {
                this.Height += m_delta;
            }
            else
            {
                this.Height -= m_delta;
            }

            OnExpandedChange(sender, new ExpandedChangeEventArgs(e.IsExpand, m_delta));
        }

        private void Header_ClickNavigationItem(object sender, ClickNavigationItemEventArgs e)
        {
            OnClickNavigationItem(sender, e);
        }
        #endregion

        #region Structure
        public NavigationStripItem()
        {
            InitializeComponent();
        }
        #endregion

        #region Inner members
        private int m_delta = 0;
        #endregion
    }
}
